
var Popup = function( title, html, buttons ){
	
	this.html = html;
	this.title = title;
	this.buttons = buttons;
	this.hg = new HtmlGenerator();
	this.background = this.hg.div('bd-popup-bg');
	this.popup;
	
	this.remove = function(){
		this.background.remove();
		this.popup.remove();
	};
		
	this.build = function(){
		var that = this;
		var wrapper = that.hg.div('bd-popup-wrapper');
		
		var closeButton = that.hg.span('close_popup');
		closeButton.click( function(){ that.remove(); });
		
		wrapper.append( closeButton );
		
		var titleSection = that.hg.div('bd-title-section').text( that.title );
		wrapper.append( titleSection );
		
		var htmlSection = that.hg.div('bd-html-section');
		htmlSection.append( that.html );
		
		wrapper.append( htmlSection );
		
		var buttonSection = that.hg.div('bd-button-section');
		for( var i in that.buttons ){
			var button = that.hg.button( that.buttons[i].value );
			if( typeof( that.buttons[i].callback ) != 'undefined' ){
				button.click( that.buttons[i].callback );				
			} else {
				button.click( function(){that.remove(); } ); 
			}
			buttonSection.append( button );
		};
		
		wrapper.append( buttonSection );
		
		return wrapper;		
	};
	
	this.display = function(){
		var that = this;
		
		$('body').append( that.background );
		
		that.popup = that.build();
		$('body').append( that.popup );
		that.center();	
	};
	
	this.center = function(){		
		var that = this;
		var winHeight = jQuery(window).height();
	    var winWidth = jQuery(window).width();
	    var height = that.popup.height();
	    var width = that.popup.width();

	    that.popup.css("left", ( winWidth - width ) / 2+jQuery(window).scrollLeft() + "px");

	    if ( height < winHeight ) {
		    that.popup.css("top", ( winHeight - height ) / 2+jQuery(window).scrollTop() + "px");
	    } else {
		    that.popup.css("top", "50px");
	    }
	    return this;
	};
};